package com.dng.book.controller;

import com.dng.book.common.Result;
import com.dng.book.dto.ResetPasswordRequest;
import com.dng.book.dto.SendSmsRequest;
import com.dng.book.service.SmsService;
import com.dng.book.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 短信控制器
 * 
 * @author dng
 * @since 2024-01-01
 */
@Slf4j
@RestController
public class SmsController {
    
    @Resource
    private SmsService smsService;
    
    @Resource
    private UserService userService;
    
    /**
     * 发送短信验证码
     * 
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/sendSms")
    public Result<String> sendSms(@RequestBody SendSmsRequest request) {
        try {
            String phone = request.getPhone();
            if (phone == null || phone.trim().isEmpty()) {
                return Result.error("手机号不能为空");
            }
            
            log.info("发送短信验证码请求: phone={}", phone);
            
            // 检查手机号是否存在
            boolean exists = userService.existsByPhone(phone);
            log.info("手机号存在检查: phone={}, exists={}", phone, exists);
            
            if (!exists) {
                log.warn("手机号未注册: phone={}", phone);
                return Result.error("该手机号未注册");
            }
            
            boolean success = smsService.sendSmsCode(phone);
            if (success) {
                log.info("发送短信验证码成功: phone={}", phone);
                return Result.success("验证码发送成功");
            } else {
                log.error("发送短信验证码失败: phone={}", phone);
                return Result.error("验证码发送失败，请稍后重试");
            }
        } catch (Exception e) {
            log.error("发送短信验证码异常", e);
            return Result.error("系统异常，请稍后重试");
        }
    }
    
    /**
     * 重置密码
     * 
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/resetPassword")
    public Result<String> resetPassword(@RequestBody ResetPasswordRequest request) {
        try {
            String phone = request.getPhone();
            String smsCode = request.getSmsCode();
            String newPassword = request.getNewPassword();
            
            if (phone == null || phone.trim().isEmpty()) {
                return Result.error("手机号不能为空");
            }
            if (smsCode == null || smsCode.trim().isEmpty()) {
                return Result.error("验证码不能为空");
            }
            if (newPassword == null || newPassword.trim().isEmpty()) {
                return Result.error("新密码不能为空");
            }
            
            // 验证短信验证码
            boolean codeValid = smsService.verifySmsCode(phone, smsCode);
            if (!codeValid) {
                return Result.error("验证码错误或已过期");
            }
            
            // 重置密码
            boolean success = userService.resetPasswordByPhone(phone, newPassword);
            if (success) {
                log.info("重置密码成功: phone={}", phone);
                return Result.success("密码重置成功");
            } else {
                log.error("重置密码失败: phone={}", phone);
                return Result.error("密码重置失败，请稍后重试");
            }
        } catch (Exception e) {
            log.error("重置密码异常", e);
            return Result.error("系统异常，请稍后重试");
        }
    }
} 